﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SMS.Business;
using SMS.DAL;

namespace SMS.Orders
{
    public partial class OrderDetails : System.Web.UI.Page
    {
        List<OrderItem> orderItems
        {
            set
            {
                if (ViewState["OrderItems"] == null)
                    ViewState["OrderItems"] = new List<OrderItem>();
                else
                    ViewState["OrderItems"] = value;
            }
            get
            {
                if (ViewState["OrderItems"] != null)
                    return (List<OrderItem>)ViewState["OrderItems"];
                else
                    return new List<OrderItem>();
            }
        }
        Order order
        {
            set
            {
                Session["order"] = value;
            }
            get
            {
                return (Order)Session["order"];
            }
        }
        User CurrentUser
        {
            get
            {
                if (Session["CurrentUser"] != null)
                    return (DAL.User)Session["CurrentUser"];
                else
                {
                    Response.Redirect("~/account/login.aspx");
                    return null;
                }
            }
        }

        private void BindDlOrderItems()
        {
            dlOrderItems.DataSource = orderItems;
            dlOrderItems.DataBind();

            decimal? totalPerItem = 0;
            OrderItem item = new OrderItem();
            for (int i = 0; i < orderItems.Count; i++)
            {
                item = orderItems[i];
                Label lblTotalPerItem = (Label)dlOrderItems.Items[i].FindControl("lblItemTotal");
                if (lblTotalPerItem != null)
                {
                    totalPerItem = item.ItemCount * item.UnitPrice;
                    lblTotalPerItem.Text = totalPerItem.ToString();
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int id = int.Parse(Request.QueryString["ID"]);
                OrderBusiness orderBus = new OrderBusiness();
                order = orderBus.GetByID(id);
                Total = order.TotalPrice;
                Paid = order.PaidPrice.Value;
                #region Order Binding
                lblID.Text = order.ID.ToString();
                if (order.Customer != null)
                    lblCustomerName.Text = order.Customer.Name;
                else
                    lblCustomerName.Text = "غيــر معـروف";
                txtDate.Text = order.Date.Value.ToString();
                clndrExtndrDate.SelectedDate = order.Date.Value;
                if (order.Type == 1)
                    lblType.Text = "شــراء";
                else
                    lblType.Text = "بيــع";
                orderItems = new List<OrderItem>();
                orderItems = order.OrderItems.ToList();
                BindDlOrderItems();
                #endregion
            }
        }

        public decimal? Total
        {
            set
            {
                ViewState["Total"] = value;
            }
            get
            {
                return (decimal)ViewState["Total"];
            }
        }
        public decimal Paid
        {
            set;
            get;
        }
        protected void dlOrderItems_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Footer)
            {
                Label lblTotalPrice = (Label)e.Item.FindControl("lblTotalPrice");
                lblTotalPrice.Text = Total.ToString();
                Label lblPaid = (Label)e.Item.FindControl("lblPaid");
                lblPaid.Text = Paid.ToString();
            }
        }

        protected void dlOrderItems_EditCommand(object source, DataListCommandEventArgs e)
        {
            if (CurrentUser.ID == order.StockUser.UserID)
            {
                dlOrderItems.EditItemIndex = e.Item.ItemIndex;
                BindDlOrderItems();
            }
        }

        protected void dlOrderItems_CancelCommand(object source, DataListCommandEventArgs e)
        {
            dlOrderItems.EditItemIndex = -1;
            BindDlOrderItems();
        }
    }
}